import React from 'react';
import * as allIcons from '@ant-design/icons';

function fixMenuItemIcon(menus, iconType = 'Outlined') {
  if (menus && menus.length > 0) {
    menus.forEach((item) => {
      const { icon, children } = item;

      if (icon && typeof icon === 'string') {

          const fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType;
          let iconObject ='';
          if(typeof allIcons[fixIconName] !== 'undefined')
          {
            iconObject = React.createElement(allIcons[fixIconName]);
          }
          else if(typeof allIcons[icon] !== 'undefined')
          {
            iconObject = React.createElement(allIcons[icon]);
          }
          item.icon = iconObject;

      }

      children && children.length > 0 ? (item.children = fixMenuItemIcon(children)) : null;
    });
  }
  return menus;
}

export default fixMenuItemIcon;
